const util = require('../../utils/util.js')

Page({
    data: {
        id: null,
        goods: {},
        quantity: 1,
        specList: [], // 规格列表
        selectedSpec: null, // 选中的规格
        selectedSpecIndex: 0, // 选中的规格索引
        currentImageIndex: 0 // 当前显示的图片索引
    },

    // 处理轮播图切换事件
    onSwiperChange(e) {
        const current = e.detail.current;
        this.setData({
            currentImageIndex: current
        });
    },
    onLoad(options) {
        const goodsList = wx.getStorageSync('goodList')
        const id = Number(options.id)
        const goods = goodsList.find(item => item.id === id)
        this.setData({
            "goods": goods,
            "id": id
        });

        // 初始化规格列表
        this.initSpecList(goods);
    },

    // 初始化规格列表
    initSpecList(goods) {
        // 模拟规格数据，实际应从商品数据中获取
        let specList = [];

        // 如果商品有规格列表
        if (goods.specList && goods.specList.length > 0) {
            specList = goods.specList;
        } else {
            // 如果没有规格列表，创建默认规格
            specList = [{
                specId: 1,
                spec: "标准规格",
                price: goods.price,
                rank: 1,
            }];
        }

        // 默认选中第一个规格
        this.setData({
            specList: specList,
            selectedSpec: specList[0],
            selectedSpecIndex: 0
        });
    },

    // 选择规格
    selectSpec(e) {
        const index = e.currentTarget.dataset.index;
        const spec = this.data.specList[index];

        this.setData({
            selectedSpec: spec,
            selectedSpecIndex: index
        });
    },

    onQuantityInput(e) {
        let quantity = parseInt(e.detail.value);
        if (isNaN(quantity) || quantity < 1) {
            quantity = 1;
        }

        this.setData({
            quantity
        });
    },

    decreaseQuantity() {
        let quantity = this.data.quantity;
        if (quantity > 1) {
            quantity--;
            this.setData({
                quantity
            });
        }
    },

    increaseQuantity() {
        let quantity = this.data.quantity;
        quantity++;
        this.setData({
            quantity
        });
    },

    // 预览图片
    previewImage(e) {
        const index = e.currentTarget.dataset.index;
        const goods = this.data.goods;
        let urls = [];
        
        // 确保pictures数组存在
        if (goods.pictures && goods.pictures.length > 0) {
            for(const picture of goods.pictures){
                urls.push(picture.fileId);
            }
            
            const current = urls[index];
            
            wx.previewImage({
                current: current, // 当前显示图片的链接
                urls: urls, // 需要预览的图片链接列表
                showmenu: true // 显示预览菜单
            });
        }
    },

    addToCart() {
        const goods = this.data.goods
        const quantity = this.data.quantity
        const selectedSpec = this.data.selectedSpec
        let cartList = wx.getStorageSync('cartList') || [];

        // 使用商品ID和规格ID组合作为唯一标识
        const cartItemId = `${goods.id}_${selectedSpec.specId}`;
        const index = cartList.findIndex(item => item.cartItemId === cartItemId);

        if (index === -1) {
            // 创建新的购物车项
            const newGoods = {
                ...goods,
                merchandiseId:goods.id,
                merchandiseName:goods.name,
                cartItemId:cartItemId,
                num: quantity,
                price: selectedSpec.price,
                specDesc: selectedSpec.spec,
                specId: selectedSpec.specId,
                checked: false
            };
            cartList.push(newGoods);
        } else {
            // 更新已有的购物车项
            cartList[index].num += quantity;
        }

        wx.setStorageSync('cartList', cartList);

        util.updateCartBadge()
        wx.showToast({
            title: '已加入购物车',
            icon: 'success'
        });
        wx.navigateBack()
    }
});